#include "..\PHQuery\query.h"
#include "..\phshared\phshared.h"
//#include "wtl\wtl.h"
#include "..\PHQuery\screen.h"
#include "PHScroll.h"
#include <boost/filesystem.hpp>

using namespace boost::posix_time;
using namespace std;
using namespace boost;

extern PHQuery phq;
extern PHDisplay phd;

bool once;

void PHTimeInfo::DoPaint(CDCHandle hDC)
{
	if(phd._complete)
	{
	CFont hFont;
	hFont.CreateFontA(12, 0, 0, 0, 400, FALSE, FALSE, 0,
                         ANSI_CHARSET, OUT_DEFAULT_PRECIS,
                         CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY,
                         DEFAULT_PITCH ,"Arial" );
	CFont oldFont=		hDC.SelectFont(hFont);

	
		DrawTimeAxis(hDC);
	
		/*if(phd.filter_user)
		{
			RECT r;
			string filterstr="Filtered on user";

			if(hDC.DrawText(filterstr.c_str(),filterstr.length(),&r,DT_CALCRECT)==0)
			PHTrace(Win32Error(),__LINE__,__FILE__);
		}
		if(phd.filter_exec)
		{
			RECT r;
			string filterstr="Filtered on executable";

			if(hDC.DrawText(filterstr.c_str(),filterstr.length(),&r,DT_CALCRECT)==0)
			PHTrace(Win32Error(),__LINE__,__FILE__);
			//TODO Draw icon, path &| version info of exec
			map<long,HICON>::iterator iconit=phd.icons.find(pit->second);
		if(iconit!=phd.icons.end())
			_hIcon=iconit->second;
		else
			_hIcon=NULL;

		if(hDC.DrawIcon(10,30,_hIcon)==0)
				PHTrace(Win32Error(),__LINE__,__FILE__);

		map<long,string>::iterator qpit=phd.qrypaths.find(_ID);
		if(qpit!=phd.qrypaths.end())
		{
			pathtxt=qpit->second;
			if ( boost::filesystem::exists(pathtxt))
			{

			GetVersionInfo(Product,Description,pathtxt);
			}}
		}*/

	if(phd._selected>0)
	{
		if(_hIcon!=NULL)
			if(hDC.DrawIcon(10,30,_hIcon)==0)
				PHTrace(Win32Error(),__LINE__,__FILE__);
		RECT r;
		r.left=0;
		r.right=1600;
		r.top=0;
		r.bottom=900;
		if(hDC.DrawText(_ps.c_str(),_ps.length(),&r,DT_CALCRECT)==0)
			PHTrace(Win32Error(),__LINE__,__FILE__);
		r.bottom+=30;
		r.right+=50;
		r.left=50;
		r.top=30;

		SIZE sz;
		sz.cx=r.right;
		sz.cy=r.bottom;

		if(once)
		{
			SetSize(sz);
			once=false;
		}
		if(hDC.DrawText(_ps.c_str(),_ps.length(),&r,DT_LEFT|DT_VCENTER)==0)
			PHTrace(Win32Error(),__LINE__,__FILE__);
		
	}
		hDC.SelectFont(oldFont);
	}
}
void PHTimeInfo::SetSize(SIZE m_size)
{		
	SetScrollOffset(0, 0, FALSE);
	SetScrollSize(m_size);
}
	/* draw with markers on the minute rather than minutes from the query start time
calculate where the first minute will be*/
void PHTimeInfo::DrawTimeAxis(  CDCHandle hDC)
{	
	time_duration wld=phd._WinLeft.time_of_day();
	int wls=wld.seconds();
	int i=0;
	i+=(60-wls);
	/* draw markers and times*/
	time_iterator titr(phd._WinLeft+seconds(i),minutes(1)); 
	
	for (; titr < phd._WinRight; ++titr) 
	{
		ptime pti=*titr;
		string zero;
		if(pti.time_of_day().minutes()>9)
			zero="";
		else	
			zero="0";
		string TimeText=lexical_cast<string>(pti.time_of_day().hours())+":"+zero+lexical_cast<string>(pti.time_of_day().minutes());
					
		if(hDC.TextOut(i-15,10,TimeText.c_str(),TimeText.length())==0)
			PHTrace(Win32Error(),__LINE__,__FILE__);
		i+=60;
	}	
}
void PHTimeInfo::DisplayInfo()
{
	sqlite3_stmt* stmt,*stmt2;
	string pathtxt,cltxt,usertxt;
	string Product,Description;
	string bintxt,total_duration;
	PHProcess pclProcess;
	time_duration ProcessDuration;
	unsigned long CRC;

	once=true;

	map<long,long>::iterator pit= phq._PData.find(_ID);
	if(pit!=phq._PData.end())
	{
		sqlite3 *db=OpenDB();
	
		/*ostringstream os;
		os<<
		"SELECT CommandLine FROM Process  JOIN CommandLines ON Process.clid=CommandLines.ID WHERE Process.ID="<<_ID<<";";
		if(sqlite3_prepare(db,os.str().c_str(),-1,&stmt,NULL)!=SQLITE_OK)
			DBError(sqlite3_errmsg(db),__LINE__,__FILE__);
		const unsigned char *commandline;
		if(sqlite3_step(stmt)== SQLITE_ROW )
		{
			commandline=sqlite3_column_text(stmt,0);
			cltxt=(char*)commandline;
		}
		sqlite3_finalize(stmt);
		sqlite3_close(db);*/
		ostringstream os2;
		os2<<"SELECT UserName,CRC FROM Process JOIN PHLogUser ON Process.UserID=PHLogUser.ID WHERE Process.ID= "<<_ID<<";";
		sqlite3 *db2=OpenDB();
		if(sqlite3_prepare(db2,os2.str().c_str(),-1,&stmt2,NULL)!=SQLITE_OK)
			DBError(sqlite3_errmsg(db),__LINE__,__FILE__);
		const unsigned char *user;
		
		if(sqlite3_step(stmt2)== SQLITE_ROW )
		{
			user=sqlite3_column_text(stmt2,0);
			usertxt=(char*)user;
			CRC=sqlite3_column_int(stmt2,1);
		}
		sqlite3_finalize(stmt2);
		sqlite3_close(db2);

		map<long,string>::iterator qpit=phd.qrypaths.find(_ID);
		if(qpit!=phd.qrypaths.end())
		{
			pathtxt=qpit->second;

			if ( boost::filesystem::exists(pathtxt))
			{

			GetVersionInfo(Product,Description,pathtxt);
		
			DWORD bin_type;
			if(GetBinaryType(pathtxt.c_str(),&bin_type)!=0)
			{
			if(bin_type==SCS_64BIT_BINARY)
				bintxt="64 bit executable";
			if(bin_type==SCS_32BIT_BINARY)
				bintxt="32 bit executable";
			}
			}
			else
				bintxt="Executable no longer exists";
		}
		//map<long,long>::iterator pathidit=phq._PData.find(_ID);
		//if(pathidit!=phq._PData.end())
		if(CRC>0)
			total_duration=PHTotals(CRC);

		map<long,PHProcess>::iterator proc_it=		phq._Processes.find(_ID);
		if(proc_it!=phq._Processes.end())
		{
			pclProcess=proc_it->second;
			ProcessDuration=pclProcess.end-pclProcess.start;
		}

		map<long,HICON>::iterator iconit=phd.icons.find(pit->second);
		if(iconit!=phd.icons.end())
			_hIcon=iconit->second;
		else
			_hIcon=NULL;
	}	

	ostringstream dlgos;
	dlgos<<"Start "<<to_simple_string(pclProcess.start)<<" End "<<to_simple_string(pclProcess.end)	
	<<" Duration "<<to_simple_string(ProcessDuration)
	<<" Total Duration: "<<total_duration<<"\r\n"
	<<"User: "<<usertxt;
	if(phd.filter_user)
		dlgos<<" FILTERED ";
	dlgos<<"\r\n"<<"Path: "<<pathtxt<<"\r\n"/*<<"Command line: "<<cltxt<<"\r\n"*/<<Product<<" "<<Description
	<<"\r\n"<<bintxt<<"\r\n"<<"CRC: "<<CRC;
	if(phd.filter_exec)
		dlgos<<" FILTERED";

	_ps=dlgos.str();
	SIZE sz;
	sz.cx=0;
	sz.cy=150;
	SetSize(sz);
}